package com.mentaleye.biz.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.PropertyBean;
import com.mentaleye.biz.PropertyBiz;
import com.mentaleye.dal.model.Property;
import com.mentaleye.service.PropertyService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import com.mentaleye.util.DateFormatter;
import com.mentaleye.util.ObjectUtil;

@Service
public class PropertyBizImpl implements PropertyBiz {

	private static Logger logger = Logger.getLogger(PropertyBiz.class);

	@Autowired
	private PropertyService dataService;

	@Override
	public CommonResult insertBean(PropertyBean dataBean) {
		CommonResult result = new CommonResult();
		dataBean.setPurchaseDate(DateFormatter.parse(dataBean.getPurchaseDateString(), "yyyy-MM-dd"));
		try {
			Property property = (Property) ObjectUtil.transfer(dataBean, Property.class);
			if (this.dataService.insertRecord(property)) {
				result.setIsSuccess(true);
				result.setResultMsg("添加财产成功！");
			} else {
				result.setResultMsg("添加财产失败！");
			}
		} catch (Exception e) {
			logger.error("添加财产异常！", e);
		}
		return result;
	}

	@Override
	public CommonResult updateBean(PropertyBean dataBean) {
		CommonResult result = new CommonResult();
		dataBean.setPurchaseDate(DateFormatter.parse(dataBean.getPurchaseDateString(), "yyyy-MM-dd"));
		try {
			Property property = (Property) ObjectUtil.transfer(dataBean, Property.class);
			if (this.dataService.updateRecord(property)) {
				result.setIsSuccess(true);
				result.setResultMsg("修改财产成功！");
			} else {
				result.setResultMsg("修改财产失败！");
			}
		} catch (Exception e) {
			logger.error("修改财产异常！", e);
		}
		return result;
	}

	@Override
	public CommonResult deleteByList(List<String> idList) {
		CommonResult result = new CommonResult();
		try {
			if (this.dataService.deleteByList(idList)) {
				result.setIsSuccess(true);
				result.setResultMsg("删除财产成功！");
			} else {
				result.setResultMsg("删除财产失败！");
			}
		} catch (Exception e) {
			logger.error("删除财产异常！", e);
		}
		return result;
	}

	@Override
	public List<Property> getList() {
		List<Property> dataList = null;
		try {
			dataList = this.dataService.getList();
		} catch (Exception e) {
			logger.error("查询财产异常！", e);
		}
		return dataList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PropertyBean> getBeanList() {
		List<PropertyBean> beanList = null;
		try {
			beanList = ObjectUtil.transferList(this.dataService.getList(), PropertyBean.class);
		} catch (Exception e) {
			logger.error("查询财产异常！", e);
		}
		return beanList;
	}

	@Override
	public PropertyBean getBeanByKey(String propertyId) {
		PropertyBean bean = null;
		try {
			bean = (PropertyBean) ObjectUtil.transfer(this.dataService.getRecordByKey(propertyId), PropertyBean.class);
		} catch (Exception e) {
			logger.error("查询财产异常！", e);
		}
		return bean;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void getListByPage(CommonPage page) {
		Map<String, Object> pageMap = null;
		List<String> strList = null;
		List<Integer> stateList = new ArrayList<Integer>();
		try {
			pageMap = page.pageToMap();
			strList = (List<String>) pageMap.get("propertyStateList");
			if (strList != null) {
				if (!strList.isEmpty()) {
					for (String str : strList) {
						stateList.add(Integer.valueOf(str));
					}
					pageMap.put("propertyStateList", stateList);
				}
			}
			page.setPageData(this.dataService.getListByMap(pageMap));
		} catch (Exception e) {
			logger.error("查询财产异常！", e);
		}
	}

}
